package com.yk.faceapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.allinpay.xed.R;
import com.yk.faceapplication.callback.ProductCallback;
import com.yk.faceapplication.entity.BillProduct;
import com.yk.faceapplication.entity.SortModel;
import com.yk.faceapplication.util.ImageUtil;
import com.yk.faceapplication.view.TagCloudLayout;

import java.util.ArrayList;
import java.util.List;


public class HotProductAdapter extends BaseAdapter {
	private Context context = null;
	private List<BillProduct> list = null;
	private ProductCallback callback;
	public HotProductAdapter(Context context, List<BillProduct> list,ProductCallback callback) {
		this.context = context;
		this.list = list;
		this.callback = callback;
	}

	@Override
	public int getCount() {
		if (list != null) return list.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (list != null) list.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_hot_product, null);
			viewHolder.img = (ImageView) convertView.findViewById(R.id.pro_img);
			viewHolder.title = (TextView) convertView.findViewById(R.id.pro_title);
			viewHolder.lable = (TextView) convertView.findViewById(R.id.pro_lab);
			viewHolder.content = (TextView) convertView.findViewById(R.id.pro_content);
			viewHolder.des = (TextView) convertView.findViewById(R.id.pro_des);
			viewHolder.tagLayout = (TagCloudLayout) convertView.findViewById(R.id.container);

			viewHolder.amount = (TextView) convertView.findViewById(R.id.amount_tv);
			viewHolder.rate = (TextView) convertView.findViewById(R.id.rate_tv);
			viewHolder.rateTag = (TextView) convertView.findViewById(R.id.rate);

			viewHolder.loanBtn = (Button) convertView.findViewById(R.id.loan_btn);
			convertView.setTag(viewHolder);
		}
		else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ImageUtil.displayImage(context, list.get(position).getProductIconId(), viewHolder.img);
		viewHolder.title.setText(list.get(position).getBaseProduct().getProductName());
		// viewHolder.lable.setText(list.get(position).getPrdLable());
//		viewHolder.content.setText(list.get(position).getPrdNote());

		viewHolder.amount.setText(list.get(position).getMinLoanAmt().split("\\.")[0]+"-"+list.get(position).getMaxLoanAmt().split("\\.")[0]);
		viewHolder.rate.setText(list.get(position).getMinLoanRate()+"%-"+list.get(position).getMaxLoanRate()+"%");
		String rateType = "利率范围";
		switch (list.get(position).getLoanRateType()) {
		case "D":
			rateType += "(日息)";
			break;
		case "M":
			rateType += "(月息)";
			break;
		case "Y":
			rateType += "(年化)";
			break;
		}
		viewHolder.rateTag.setText(rateType);
//		viewHolder.des.setText("申请人数:" + list.get(position).getWantRen() + "人    " + rateType + list.get(position).getRateMin() + "%");
		if(list.get(position).getLoanLabel() != null){
			List<String> mList = new ArrayList<>();
			for (String str : list.get(position).getLoanLabel().split("\\|")) {
				mList.add(str);
			}
			TagBaseAdapter mAdapter = new TagBaseAdapter(context, mList);
			viewHolder.tagLayout.setAdapter(mAdapter);
		}
		// viewHolder.mGridView.setAdapter(new TextAdapter(context,
		// list.get(position).getPrdLable().split(",")));

//		convertView.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//
//
//			}
//		});
		viewHolder.loanBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SortModel sortModel = new SortModel();
				sortModel.setUid(list.get(position).getLoanShowId());
				callback.clickItem(sortModel);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private ImageView img = null;
		private TextView title = null;
		private TextView lable = null;
		private TextView content = null;
		private TagCloudLayout tagLayout = null;
		private TextView des = null;

		private TextView rate = null;
		private TextView amount = null;
		private TextView rateTag = null;
		private Button loanBtn;
	}

}
